package com.efounder.activity.Order;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.efounder.adapter.BaseAdapter;
import com.efounder.bean.OrderInfoBean;
import com.efounder.holder.OrderPreHolder;
import com.efounder.mobilemall.R;
import com.efounder.util.DialogUtil;
import com.efounder.util.ToastUtil;
import com.utilcode.util.Utils;

import java.util.List;

/**
 * Created by Marcello on 2018/9/19.
 */
public class OrderPreAdapter extends BaseAdapter<OrderInfoBean.DataBean.PreOrderDiscountBean, OrderPreHolder> {
    /**
     * 设置数据,并设置点击回调接口
     *
     * @param list 数据集合
     */
    public OrderPreAdapter(@Nullable List list) {
        super(list);
    }

    @Override
    public OrderPreHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderPreHolder(parent, R.layout.item_order_pre_dec);
    }

    @Override
    public void onBindViewHolder(OrderPreHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        onOrderPreHolderClick(holder, position);

    }

    private void onOrderPreHolderClick(final OrderPreHolder holder, final int position) {
        TextView orderTvChangeNum = holder.getOrderTvChangeNum();
        Context context = holder.getContext();
        if (orderTvChangeNum != null) {
            editDialog(holder, position, orderTvChangeNum, context);
        }
        SuperTextView order_stv_preferent = holder.getOrder_stv_preferent();
        if (order_stv_preferent != null) {
            order_stv_preferent.setSwitchCheckedChangeListener(new SuperTextView.OnSwitchCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (mOnOrderPreHolderListener != null) {
                        mOnOrderPreHolderListener.onCheckboxChanged(compoundButton, b, position, holder);
                    }
                }
            });
        }
    }

    private void editDialog(final OrderPreHolder holder, final int position, TextView orderTvChangeNum, final Context context) {
        orderTvChangeNum.setOnClickListener(new View.OnClickListener() {//输入银钻数量
            @Override
            public void onClick(View v) {
                final Dialog dialog = DialogUtil.customDialogWithLayout(context, R.layout.dialog_edit_mall, 1, false);
                final EditText editText = dialog.findViewById(R.id.dialog_edt);
                Button sure = dialog.findViewById(R.id.mall_dialog_order_sure);
                Button cancle = dialog.findViewById(R.id.mall_dialog_order_cancle);
                TextView title = dialog.findViewById(R.id.tv_confirm_dialog_title);
                final OrderInfoBean.DataBean.PreOrderDiscountBean item = getItem(position);
                if (item != null) {
                    String string = Utils.getApp().getResources().getString(R.string.dialog_title_order);
                    title.setText(String.format(string, item.getDiscountTypeName()));
                    String str = Utils.getApp().getResources().getString(R.string.dialog_edit_hint);
                    if (item.getAtMostAmount() < item.getHasAmount()) {
                        editText.setHint(String.format(str, item.getAtMostAmount() + ""));
                    } else {
                        editText.setHint(String.format(str, item.getHasAmount() + ""));
                    }
                    editText.setText(holder.getOrderTvChangeNum().getText().toString());
                    editText.setSelection(holder.getOrderTvChangeNum().getText().toString().length());
                }

                sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String trim = editText.getText().toString().trim();
                        double anInt = 0.00;
                        if (TextUtils.isEmpty(trim)) {

                        } else {
                            anInt = Double.valueOf(trim);
                        }

                        if (item != null && item.getHasAmount() < anInt) {
                            String string = context.getResources().getString(R.string.pre_not_enought);
                            ToastUtil.showToastCenter(String.format(string, item.getDiscountTypeName()));
                            return;
                        } else if (item.getAtMostAmount() < anInt) {
                            String string = context.getResources().getString(R.string.pre_than_max);
                            ToastUtil.showToastCenter(String.format(string, item.getAtMostAmount()+"", item.getDiscountTypeName()));
                            return;
                        }
                        dialog.dismiss();
                        if (mOnOrderPreHolderListener != null) {
                            mOnOrderPreHolderListener.onPreferentChange(holder, anInt, position);
                        }
                    }
                });
                cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if(s.toString().trim().equals(".") && s.toString().trim().length()==1){//自动补零
                            s.insert(0,"0");
                        }
                    }
                });

                dialog.show();
            }
        });
    }

    public interface OnOrderPreHolderListener { //方便扩展
        void onPreferentChange(OrderPreHolder holder, double trim, int position);

        void onCheckboxChanged(CompoundButton buttonView, boolean isChecked, int pos, OrderPreHolder holder);
    }

    private OnOrderPreHolderListener mOnOrderPreHolderListener;

    public void setOnOrderPreHolderListener(OnOrderPreHolderListener mOnOrderPreHolderListener) {
        this.mOnOrderPreHolderListener = mOnOrderPreHolderListener;
    }
}
